import { describe } from '$lib';
import { Description, Feature, Glyph, Interesting } from '$lib/components';
import { Types, defineComponent } from '$lib/ecs';
import { log } from '$lib/log';
import { registerLocationTrigger } from '$lib/pos3';
import { randomChoice } from '$lib/random';
import { logVisible } from '$lib/render3';
import { applyBleed, enumBodyParts } from './body';

const BleedTrap = defineComponent('BleedTrap', {
  amount: Types.ui16,
});

export const bleedTrapTemplate = [
  [Feature],
  [Interesting],
  [BleedTrap, {
    amount: 1,
  }],
  [Description, {
    summary: 'bleed trap',
    description: "Induces unconditional bleed. Placed by some very powerful entity not belonging to this world",
  }],
  [Glyph, {
    glyph: '^',
    color: '#b22',
  }],
];

registerLocationTrigger({
  target: BleedTrap,
  trigger: (world, trap, e) => {
    log('chuck');
    const amount = BleedTrap.amount[trap];
    logVisible(e, `${describe(world, e)} bleeds!`);
    const bodyPart = randomChoice(enumBodyParts(world, e));
    applyBleed(world, e, bodyPart, 1);
  },
});
